﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.WindowsAPICodePack.Shell;
using System.Threading;
using System.ComponentModel;

namespace CrazySearch
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            StartSearch(SearchText.Text);
        }

        ShellSearchFolder result;

        private void StartSearch(string searchtext)
        {
            if (searchtext == null || searchtext == string.Empty)
            {
                return;
            }

            SearchCondition searchCondition =
                SearchConditionFactory.ParseStructuredQuery(searchtext);

            //ShellSearchFolder searchFolder =
            //    new ShellSearchFolder(
            //        searchCondition,
            //        (ShellContainer)KnownFolders.Libraries);

            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (s, e) =>
                {
                    ShellSearchFolder searchFolder =
                    new ShellSearchFolder(
                        searchCondition,
                        (ShellContainer)KnownFolders.Libraries);

                    result = searchFolder;
                };
            bw.RunWorkerCompleted += (s,e) =>
                {
                    SearchResult.ItemsSource = result;
                };
            bw.RunWorkerAsync();

            //try
            //{
                
            //}
            //catch
            //{
            //    searchFolder.Dispose();
            //    searchFolder = null;
            //}
        }
    }
}
